from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

class BlogList:
    url=""
    driver=""

    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_list.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    #登录状态下测试博客首页
    def ListTestBylogin(self):
        #标题是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.title")
        #内容是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.desc")
        #时间是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > div.date")
        #昵称是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        #头像是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")
        #查看全文按钮是否存在，是否可以跳转到博客详情页
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(1) > a").click()
        excepted_url="http://8.137.19.140:9090/blog_detail.html?blogId=24712"
        current_url=self.driver.current_url
        if current_url==excepted_url:
            print(f"查看全文按钮跳转成功！")
        else:
            print(f"查看全文按钮跳转失败！")
        self.driver.back()

        #注销按钮是否存在，是否可以跳转到博客登录页
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()
        excepted_url = "http://8.137.19.140:9090/blog_login.html"
        current_url = self.driver.current_url
        if current_url == excepted_url:
            print(f"注销按钮跳转成功！")
        else:
            print(f"注销按钮跳转失败！")
        self.driver.back()

        #写博客按钮是否存在，是否可以跳转到博客编辑页
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        excepted_url = "http://8.137.19.140:9090/blog_edit.html"
        current_url = self.driver.current_url
        if current_url == excepted_url:
            print(f"写博客按钮跳转成功！")
        else:
            print(f"写博客按钮跳转失败！")
        self.driver.back()

        #添加屏幕截图
        BlogDriver.getScreenShot()
